package com.rockbb.common.utilities.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.List;

import org.apache.log4j.Logger;

import com.rockbb.common.config.Config;


public class VideoUtil 
{
	public static final String ffmpegPath = Config.getValue("ffmpeg.env_path");
	private static Logger logger = Logger.getLogger(VideoUtil.class);

	/**
	 * Capture the screenshot of input video by specifying the seconds
	 * 
	 * @param sourcePath
	 * @param targetPath		Note: No matter what name and extension are, the picture format is always JPEG
	 * @param seconds			The seconds can not be large because it will take time to play the frames. Less than 30 is recommended.
	 * @return
	 */
	public static boolean videoToImageByFFmpeg(String sourcePath, String targetPath, double seconds)
	{
		File file = new File(sourcePath);
		if (!file.isFile()) return false;

		/*
		 * ffmpeg
		 * -y 		Overwrite output files without asking. 
		 * -f			Force input or output file format.
		 * -i			Input file name
		 * -s			Set frame size. As an output option, this inserts the scale video filter to the end of the corresponding 
		 * 			filtergraph, The format is ‘wxh’
		 * -ss		Position. May be either in seconds or in hh:mm:ss[.xxx] form
		 * -itsoffset	Set the input time offset in seconds. [-]hh:mm:ss[.xxx] syntax is also supported. A positive offset means 
		 * 				that the corresponding streams are delayed by offset seconds. 
		 * -vframes number (output)	Set the number of video frames to record. This is an alias for -frames:v.
		 * -an (output)							Disable audio recording.
		 * -vcodec codec (output)		Set the video codec. This is an alias for -codec:v. 
		 *  -t duration (output)				Stop writing the output after its duration reaches duration. duration may be a number 
		 *  											in seconds, or in hh:mm:ss[.xxx] form.
		 */
		// ffmpeg -itsoffset -4 -i test.avi -vcodec mjpeg -vframes 1 -an -f rawvideo -s 320x240 test.jpg
		// ffmpeg -itsoffset -4 -i test.flv -y -t 0.001 -an -f image2 -s 320x240 b.jpg
		// ffmpeg -itsoffset -4 -i test.avi -vcodec mjpeg -vframes 1 -y -an -f rawvideo out3.jpg
		List<String> command = new java.util.ArrayList<String>();
		command.add(ffmpegPath + "/ffmpeg");
		command.add("-itsoffset");
		command.add("-" + seconds);
		command.add("-i");
		command.add(sourcePath);
		command.add("-vcodec");
		command.add("mjpeg");
		command.add("-vframes");
		command.add("1");
		command.add("-y");
		command.add("-an");
		command.add("-f");
		command.add("rawvideo");
		command.add(targetPath);
		try 
		{
			logger.info("Video to image converting: " + command.toString());
			ProcessBuilder builder = new ProcessBuilder();
			builder.command(command);
			builder.redirectErrorStream(true);
			Process process = builder.start();
			BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
			String line;
			while ((line = reader.readLine()) != null)
			    System.out.println("tasklist: " + line);
			process.waitFor();
			logger.info("Video to image converting: " + command.toString() + " DONE");
			return true;
		}
		catch (Exception e) 
		{
			logger.error("Unknown Exception in video to image converting");
			logger.debug(e.getMessage(), e);
		}
		return false;
	}

	public static void main(String[] args)
	{
		videoToImageByFFmpeg(
				"C:\\webserver\\wwwpcomroot\\cn\\files\\static\\2013\\11\\201311020257_0d1f0332.flv", 
				"C:\\webserver\\wwwpcomroot\\cn\\files\\static\\2013\\11\\201311020257_0d1f0332B.jpg",
				13.5f);
	}
}
